/**
 * Created by YUJO on 8/7/2016.
 */
function showShipmentDetail(obj, divId) {
    $("#"+divId+"").toggle();

    if($(obj).find(".glyphicon-menu-up").is(':hidden')){

        $(obj).find(".glyphicon-menu-down").hide();
        $(obj).find(".glyphicon-menu-up").show();
        $("#"+divId+"").parent(".oal-my-shipment-list-item").addClass("highlight-border");
    }
    else{
        $(obj).find(".glyphicon-menu-up").hide();
        $(obj).find(".glyphicon-menu-down").show();
        $("#"+divId+"").parent(".oal-my-shipment-list-item").removeClass("highlight-border");
    }
}


$(document).ready(function(){
    //checkForm();
    $('.booking-success').hide();
    $('.oal-booking-preview-section').hide();
    //$('.booking-door-info').hide();
    $('.oal-schedule-list').hide();


    $("input[name='rdOBhaulage']").change( function() {
        if($(this).val()=='Door'){
            $('.booking-door-info').slideDown();
        }
        else{
            $('.booking-door-info').slideUp();
        }
    });

    //show detail button
    //$(".glyphicon-menu-up").hide();


    $('#search_date_from').val(moment());
    $('#search_date_from').dateRangePicker({
        autoClose: true,
        singleDate : true,

        //singleMonth: true,
        format: 'DD MMM YYYY',
        language: 'auto'
    });




    $('#search_date_door1').dateRangePicker({
        autoClose: true,
        singleDate : true,
        singleMonth: true,
        format: 'DD MMM YYYY hh:mm A',
        language: 'auto',
        time: {
            enabled: true
        }
    });

    $('#search_date_door2').dateRangePicker({
        autoClose: true,
        singleDate : true,
        singleMonth: true,
        format: 'DD MMM YYYY hh:mm A',
        language: 'auto',
        time: {
            enabled: true
        }
    });

    $('#search_date_door3').dateRangePicker({
        autoClose: true,
        singleDate : true,
        singleMonth: true,
        format: 'DD MMM YYYY hh:mm A',
        language: 'auto',
        time: {
            enabled: true
        }
    });



    $(".oal-datepicker .input-group-addon").bind("click",function(evt){
        evt.stopPropagation();
        $(this).parent().find("input").data('dateRangePicker').open();

    });
    var city = [
        {"name":"Sowerby Bridge (GB)"},
        {"name":"Hong kong (HK)"},
        {"name":"Shanghai (CN)"},
        {"name":"Shenzhen (CN)"},
    ]
/*
    $("#search_por,#search_fnd").combobox({
        data:city,
        minLength:0,
        match:'name',
        getSelected:function(item){

        }
    });
    */

    $('.oal-schedule-list-item .btn-book-now').bind("click",function(){


        $('.oal-booking-section').hide();
        $('.oal-booking-preview-section').show();



    });


    $('#btnBacktoBooking').bind("click",function(){
        $('.oal-booking-section').show();
        $('.oal-booking-preview-section').hide();
        $('.booking-success').hide();
        $('.oal-booking-preview-section .oal-page-content-setting-action-bar').show();



    });
    $('#btnSearchBook').bind("click",function(){
        checkForm();
    });

    function jumpUrl(count) {
        window.setTimeout(function(){
            count--;
            if(count > 0) {
                $(".second-count").text(count);
                jumpUrl(count);
            } else {
                location.href= 'homepage.html';
            }

        }, 1000);
    }


    $('#btnConfirmBooking').bind("click",function(){
        $('.booking-success').slideDown();

        $('.oal-booking-preview-section .oal-page-content-setting-action-bar').hide();

        jumpUrl(10);
    });

    $('#search_por, #search_fnd').blur( function () {
        var porStatus = $('#search_por').val();
        var fndStatus = $('#search_fnd').val();

        if($(this).hasClass('required-field')){
            if($(this).val()==''){
                $(this).addClass('error-field');
            }else{
                $(this).removeClass('error-field');
            }
        }
        if(porStatus !="" && fndStatus !=""){
            $('.oal-schedule-list').slideDown();
        }
    });


});

function checkForm(){
    var passed = true;
    $('.search-fields input').each(function(i){
        if($(this).hasClass('required-field')){
            if($(this).val()==''){
                $(this).addClass('error-field');
                passed = false;
            }
            else{
                $(this).removeClass('error-field');
            }

        }
    });

    //check container
    var hascontainer = false;
    $('.oal-booking-container-list >.oal-booking-container-list-item').each(function(i){
        var containerFilled = false;
        $(this).find('input').each(function(j){
            if(j==0){
                if($(this).val()>0){
                    containerFilled = true;
                    hascontainer = true;
                }
            }else if(containerFilled){
                if($(this).val()===''){
                   $(this).addClass('error-field');

                    passed = false;
                }
                else{
                    $(this).removeClass('error-field');
                }

            }
        })
    })

    if(hascontainer && passed){
        $('.oal-schedule-list').slideDown();
    }

}







